﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EventStandsApp
{
    public class Client
    {
        //fields
        private int clientId;
        private String lastName;
        private String firstName;
        private double balance;
        private double virtualBalance;
        List<RentedItem> itemsRented;
        DataHelper dh = new DataHelper();
        //Constructor
        public Client(int id, String lastName, String firstName, double balance)
        {
            this.clientId = id;
            this.lastName = lastName;
            this.firstName = firstName;
            this.balance = balance;
            this.virtualBalance = balance;
            itemsRented = new List<RentedItem>();
            itemsRented = dh.loadRentedItems(id);
        }
        //Reloads the items in the list 
        public void reloadRentedItems()
        {
            itemsRented = dh.loadRentedItems(clientId);
        }
        //Getters and setters
        public int ClientId
        {
            get { return this.clientId; }
        }
        public String LastName
        {
            get { return this.lastName; }
        }
        public String FirstName
        {
            get { return this.firstName; }
        }
        public double Balance
        {
            get { return this.balance; }
            set { this.balance = value; }
        }
        public double VirtualBalace
        {
            get { return this.virtualBalance; }
            set { this.virtualBalance = value; }
        }
       /*
        * returns a list with items that are returned
        * if no such items, it will return an empty list*/
        public List<RentedItem> getReturnedItems()
        {
            List<RentedItem> l = new List<RentedItem>();
            for (int i = 0; i < itemsRented.Count; i++)
            {
                if (itemsRented[i].IsReturned == true)
                {
                    l.Add(itemsRented[i]);
                }
            }
            return l;
        }
        /*
         * returns a list with items that are not returned
         * if no such items, it will return an empty list*/
        public List<RentedItem> getUnreturnedItems()
        {
            List<RentedItem> l = new List<RentedItem>();
            for (int i = 0; i < itemsRented.Count; i++)
            {
                if (itemsRented[i].IsReturned == false)
                {
                    l.Add(itemsRented[i]);
                }
            }
            return l;
        }

        /*
         * set the field HasReturned to true for a certain product
         * returns true if item was succesfully returned and false otherwise*/
        public bool returnItem(int productId)
        {
            int ok = 0;
            for (int i = 0; i < itemsRented.Count; i++)
            {
                if (itemsRented[i].ProductId == productId && itemsRented[i].IsReturned == false && ok==0)
                {
                    itemsRented[i].IsReturned = true;
                    ok = 1;
                    if (dh.returnItem(clientId, itemsRented[i]) > -1)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        /*
         * updates the balance of the client
         * returns true if operation succeeded or false otherwise*/
        public bool updateBalace()
        {
            if (dh.updateBalace(clientId,balance) > -1)
            {
                return true;
            }
            return false;
        }
        /*
         * returns an item based on its id
         * if no such item is found it returns null*/
        public RentedItem getItem(int prodId)
        {
            for (int i = 0; i < itemsRented.Count; i++)
            {
                if (itemsRented[i].ProductId == prodId)
                {
                    return itemsRented[i];
                }
            }
            return null;
        }
        
    }
}
